package com.mc.moring.information.adapter;


import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mc.moring.R;
import com.mc.moring.bean.EmergencyBean;
import com.mc.moring.bean.ZiLiPageUserDataBean;
import com.mc.moring.util.PointMgr;

import java.util.List;
import java.util.regex.Pattern;

public class EmergencyAdapter extends BaseQuickAdapter<EmergencyBean, BaseViewHolder> {

    EmergencyListener emergencyListener;

    public EmergencyAdapter(@Nullable List<EmergencyBean> data) {
        super(R.layout.item_emergency, data);
    }

    public interface EmergencyListener {
        void relationShips(int id);

        void updataName(int id, String name);

        void telephoneNumber(int id, String phone);

        void removeEmergencyItem(int position);

    }

    public void setEmergencyListener(EmergencyListener emergencyListener) {
        this.emergencyListener = emergencyListener;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, EmergencyBean bean) {
        ImageView iv_close = baseViewHolder.getView(R.id.iv_close);
        if (baseViewHolder.getPosition() > 1) {
            iv_close.setVisibility(View.VISIBLE);
        } else {
            iv_close.setVisibility(View.GONE);
        }
        baseViewHolder.setIsRecyclable(false);
        EditText name = baseViewHolder.getView(R.id.et_emergency_name);
        EditText relationships = baseViewHolder.getView(R.id.et_relationship_kinship);
        EditText telephoneNumber = baseViewHolder.getView(R.id.et_telephone_number);
        FrameLayout flRelationships = baseViewHolder.getView(R.id.fl_relationship_kinship);
        TextView tv_name_err = baseViewHolder.getView(R.id.tv_name_err);
        View tv_name_line = baseViewHolder.getView(R.id.tv_contact_line);
        TextView tvTelephoneNumberErr = baseViewHolder.getView(R.id.tv_telephone_number_err);
        View tv_telephone_number_line = baseViewHolder.getView(R.id.tv_telephone_number_line);
        TextView tv_relationship_err = baseViewHolder.getView(R.id.tv_relationship_err);
        View tv_relationship_line = baseViewHolder.getView(R.id.tv_relationship_line);
        if (!TextUtils.isEmpty(bean.getPhoneNumber())) {
            telephoneNumber.setText(bean.getPhoneNumber());
        }
        if (!TextUtils.isEmpty(bean.getName())) {
            name.setText(bean.getName());
        }
        if (!TextUtils.isEmpty(bean.getRelationship())) {
            if (bean.getRelationship().equals("A")) {
                relationships.setText("Padres");
            } else if (bean.getRelationship().equals("B")) {
                relationships.setText("Cónyuge");
            } else if (bean.getRelationship().equals("C")) {
                relationships.setText("Hijos");
            } else if (bean.getRelationship().equals("D")) {
                relationships.setText("Hermanos");
            } else if (bean.getRelationship().equals("E")) {
                relationships.setText("Amigo");
            } else {
                relationships.setText("Colega");
            }
        }
        if (bean.isShowNameErr()) {
            tv_name_err.setVisibility(View.VISIBLE);
            tv_name_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
        } else {
            tv_name_err.setVisibility(View.GONE);
            tv_name_line.setBackgroundColor(Color.parseColor("#33000000"));
        }

        if (TextUtils.isEmpty(bean.getNumErr())) {
            tvTelephoneNumberErr.setVisibility(View.GONE);
            tv_telephone_number_line.setBackgroundColor(Color.parseColor("#33000000"));
        } else {
            tvTelephoneNumberErr.setVisibility(View.VISIBLE);
            tvTelephoneNumberErr.setText(bean.getNumErr());
            tv_telephone_number_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
        }

        if (bean.isShowRelationShipErr()) {
            tv_relationship_err.setVisibility(View.VISIBLE);
            tv_relationship_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
        } else {
            tv_relationship_err.setVisibility(View.GONE);
            tv_relationship_line.setBackgroundColor(Color.parseColor("#33000000"));
        }

        name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

                String trimmedText = charSequence.toString().trim();

                if (!trimmedText.isEmpty()) {
                    tv_name_err.setVisibility(View.GONE);
                    tv_name_line.setBackgroundColor(Color.parseColor("#33000000"));
                }

                if (startsWithSpace(charSequence)) {

                    name.setText(trimmedText);
                    name.setSelection(trimmedText.length());
                } else {
                    tv_name_err.setVisibility(View.GONE);
                    tv_name_line.setBackgroundColor(Color.parseColor("#33000000"));
                }


                if (charSequence.length() > 128) {
                    tv_name_err.setVisibility(View.VISIBLE);
                    tv_name_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                } else {
                    tv_name_err.setVisibility(View.GONE);
                    tv_name_line.setBackgroundColor(Color.parseColor("#33000000"));
                }


                if (hasChinese(charSequence.toString())) {
                    tv_name_err.setVisibility(View.VISIBLE);
                    tv_name_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                } else {
                    tv_name_err.setVisibility(View.GONE);
                    tv_name_line.setBackgroundColor(Color.parseColor("#33000000"));
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {
                for (int i = 0; i < editable.length(); i++) {
                    char c = editable.charAt(i);
                    if (hasChinese(String.valueOf(c))) {
                        editable.delete(i, i + 1);
                    }
                }
                emergencyListener.updataName(bean.getId(), editable.toString());
            }

            private boolean startsWithSpace(CharSequence charSequence) {

                return charSequence.length() > 0 && charSequence.charAt(0) == ' ';
            }

        });
        noChinese(name);

        ZiLiPageUserDataBean userDataBean = ZiLiPageUserDataBean.getInstance();
        String mobile = userDataBean.getMobile();
        telephoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                String trimmedText = charSequence.toString().trim();

                if (startsWithSpace(charSequence)) {

                    telephoneNumber.setText(trimmedText);
                    telephoneNumber.setSelection(trimmedText.length());
                }

                if ( trimmedText.length() == 9) {
                    tvTelephoneNumberErr.setVisibility(View.GONE);
                    tv_telephone_number_line.setBackgroundColor(Color.parseColor("#33000000"));
                }
            }

            private boolean startsWithSpace(CharSequence charSequence) {

                return charSequence.length() > 0 && charSequence.charAt(0) == ' ';
            }

            @Override
            public void afterTextChanged(Editable editable) {
                for (int i = 0; i < editable.length(); i++) {
                    char c = editable.charAt(i);
                    if (hasChinese(String.valueOf(c))) {
                        editable.delete(i, i + 1);
                    }
                }
                emergencyListener.telephoneNumber(baseViewHolder.getPosition(), editable.toString());
            }
        });
        noChinese(telephoneNumber);
        telephoneNumber.setFilters(new InputFilter[]{new InputFilter.LengthFilter(9)});

        flRelationships.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_relationship_err.setVisibility(View.GONE);
                tv_relationship_line.setBackgroundColor(Color.parseColor("#33000000"));
                if (emergencyListener != null) {
                    emergencyListener.relationShips(baseViewHolder.getPosition());
                }
            }
        });

        iv_close.setOnClickListener(v -> {
            PointMgr.INSTANCE.sendEvent("cl_marital_3_status_click_yes");
            Log.e("PointMgr.INSTANCE.sendEvent", "cl_marital_3_status_click_yes");
            emergencyListener.removeEmergencyItem(baseViewHolder.getPosition());
        });
    }

    public static boolean hasChinese(String input) {
        String regex = "[\u4e00-\u9fa5]";
        return Pattern.matches(".*" + regex + ".*", input);
    }

    private void noChinese(EditText editText){
        editText.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence charSequence, int start, int end, Spanned spanned, int i2, int i3) {
                for (int i = start; i < end; i++) {
                    if (Character.UnicodeBlock.of(charSequence.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }
}
